package com.lanyou.esb.web;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lanyou.cook.common.Constants;
import com.lanyou.cook.repository.FieldSystemRepository;
import com.lanyou.esb.common.ExceptionCode;
import com.lanyou.esb.datadictionary.AuditStatusType;
import com.lanyou.esb.datadictionary.EnableType;
import com.lanyou.esb.datadictionary.IfStatusType;
import com.lanyou.esb.entity.Ftp;
import com.lanyou.esb.exception.AuthException;
import com.lanyou.esb.repository.FtpRepository;
import com.lanyou.esb.servicemix.util.FtpRouteHandleUtil;


@RestController
public class FtpUnSchedulTriggerController {
	
	@Autowired
	private FieldSystemRepository fieldSystemRepository;
	
	@Autowired
	private FtpRepository ftpRepository;

	@RequestMapping(value="/ftp/{*}/{*}/{*}")
	public void startRoute(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 系统编码
		String systemCode = null;
		// 接口版本
		String ifVersion = null;
		// 接口编码
		String ifCode = null;

		try{
			/**
			 * 接口请求地址的统一规范格式: http://{domain}/{systemcode}/{version}/{interface_code
			 * }/{query_string}
			 ***/
			String[] patterns = request.getRequestURI().substring(1).split("/");
			systemCode = patterns[1];
			ifVersion = patterns[2];
			ifCode = patterns[3];
		}catch(Exception e){
			response.sendError(HttpServletResponse.SC_BAD_REQUEST);
		}
		
		try{
			long num = fieldSystemRepository.countByCode(systemCode);
			if(num == 0){
				//找不到系统
				throw new AuthException(ExceptionCode.MSG_NO_SYSTEM,
						ExceptionCode.CODE_NO_SYSTEM);
			}
			
			Ftp ftp = ftpRepository.findByInterfacecodeAndVersionAndIsEnable(ifCode, ifVersion, EnableType.ENABLE.ValueCode);
			if(ftp == null){
				//找不到接口
				throw new AuthException(ExceptionCode.MSG_NO_INTERFACE,
						ExceptionCode.MSG_NO_INTERFACE);
			}
			//已被卸载
			if(StringUtils.equals(ftp.getIsEnable(), EnableType.DISABLE.ValueCode)){
				throw new AuthException(ExceptionCode.MSG_IF_DISABLE,
						ExceptionCode.CODE_IF_DISABLE);
			}
			
			//未审核
			if(StringUtils.equals(ftp.getAuditStatus(), AuditStatusType.UNAUDITED.ValueCode)){
				throw new AuthException(ExceptionCode.MSG_UNAUDITED,
						ExceptionCode.CODE_UNAUDITED);
			}
			
			//已停用
			if(StringUtils.equals(ftp.getStatus(), IfStatusType.DISABLE.ValueCode)){
				throw new AuthException(ExceptionCode.MSG_IF_UNUSE,
						ExceptionCode.CODE_IF_UNUSE);
			}
		
			boolean status = FtpRouteHandleUtil.process(Constants.RESTART_CAMEL, ifCode);
			if(status){
				response.setStatus(HttpServletResponse.SC_OK);
			}else{
				throw new AuthException(ExceptionCode.MSG_REQUEST_ROUTE_FAILD,
						ExceptionCode.CODE_REQUEST_ROUTE_FAILD);
			}
		}catch(AuthException e){
			// 设置错误返回
			response.sendError(HttpServletResponse.SC_UNAUTHORIZED, e.getMessage());
		}
	}
}
